<?php

namespace App\Models;


class Address extends BaseModel
{
    protected $table = "address";

    protected $rules = [
        'name' => 'required|between:1,5',
        'mobile' => 'required|regex:/^1[3456789]{1}\d{9}$/',
        'province_id' => 'required|digits:6',
        'city_id' => 'required|digits:6',
        'area_id' => 'digits:6',
        'area_info' => 'required',
        'address' => 'required|between:1,50',
    ];

    protected $message = [
        'name.required' => '收货人姓名必须填写',
        'name.between' => '收货人姓名不能超过5个字',
        'mobile.required' => '联系方式必须填写',
        'mobile.regex' => '请输入正确的手机号',
        'province_id.required' => '请选择所属省份',
        'province_id.digits' => '非法数据',
        'city_id.required' => '请选择所属区域',
        'city_id.digits' => '非法数据',
        'area_id.digits' => '非法数据',
        'area_info.required' => '请选择所属地区',
        'address.required' => '详细地址必须填写',
        'address.between' => '详细地址不能超过50个字',
    ];

    public function member()
    {
        return $this->belongsTo('App\Models\Member', 'mid', 'id');
    }

    public function cash()
    {
        return $this->hasOne('App\Models\MemberCash', 'mid', 'id');
    }

    public function memberWaterBill()
    {
        return $this->hasMany('App\Models\MemberWaterBill', 'mid', 'id');
    }

    public function memberCommission()
    {
        return $this->hasOne('App\Models\MemberCommission','mid','mid');
    }


    public function addOrUpdate()
    {
        $data['mid'] = trim($GLOBALS['mid']);
        $data['shop_id'] = trim($GLOBALS['shop_id']);
        $data['admin_id'] = trim($GLOBALS['admin_id']);
        $data['name'] = trim(request('name'));
        $data['mobile'] = trim(request('mobile'));
        $data['type'] = intval(request('type'));
        $data['company_name'] = trim(request('company_name'));
        if($data['type'] < 1) {
            $this->error = '请选择用户类型';
            return false;
        }
        if($data['type'] == 1 && !$data['company_name']) {
            $this->error = '请填写公司名称';
            return false;
        }
        $data['province_id'] = request('province_id');
        $data['city_id'] = request('city_id');
        $data['area_id'] = request('area_id');
        $data['area_info'] = request('area_info');
        $data['address'] = trim(request('address'));
        $data['area_position'] = request('area_position','');
        $data['position_address'] = request('position_address','');
        if (request('id')) {
            $data['id'] = trim(request('id'));
        } else {
            $num = $this->_count(['mid' => $data['mid']]);
            if ($num >= 5) {
                $this->error = '收货地址最多添加5个!';
                return false;
            }
            if (!$num) {
                $data['is_default'] = 1;
            }
        }
        if ($data['area_info'] && is_array($data['area_info'])) {
            $data['area_info'] = implode(' ', $data['area_info']);
        }
        if (!$this->_validate($data)) {
            return false;
        }
        $res = $this->_save($data);
        if (!$res) {
            $this->error = '操作失败，请稍后再试!';
            return false;
        }
        return '保存成功！';
    }

    public function setDefault()
    {
        $id = request('id');
        if (!$id) {
            $this->error = '操作失败，请选择要操作的数据!';
            return false;
        }
        $address = $this->_getOneById($id);
        if ($address['is_default']) {
            return true;
        }
        $where['shop_id'] = $GLOBALS['shop_id'];
        $where['mid'] = $GLOBALS['mid'];
        $this->_start();
        try {
            $this->_update($where, ['is_default' => 0]);
            $this->_update(['id' => $id], ['is_default' => 1]);
            $this->_commit();
            return '操作成功';
        } catch (\Exception $e) {
            $this->_rollBack();
            $this->error = '操作失败，请稍后再试!';
            return false;
        }
    }

    public function del()
    {
        $id = request('id');
        if (!$id) {
            $this->error = '请选择要操作的数据!';
            return false;
        }
        $res = $this->_del(['id' => $id]);
        if (!$res) {
            $this->error = '删除失败!';
            return false;
        }
        $num = $this->_count(['mid' => $GLOBALS['mid']]);
        if ($num == 1) {
            $this->_update(['mid' => $GLOBALS['mid']], ['is_default' => 1]);
        }
        return '删除成功';
    }

}
